package cn.sxt.clinic.controller;


import cn.sxt.clinic.pojo.Dictionary;
import cn.sxt.clinic.pojo.ResultCode;
import cn.sxt.clinic.service.IDictionaryService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-29
 */
@Controller
@RequestMapping("/dictionary")
public class DictionaryController {

    @Autowired
    private IDictionaryService dictionaryService;

    @RequiresPermissions("dictionary:dictionaryManager")
    @RequestMapping("/dictionaryManager")
    public String dictionaryManager(){
        return "dictionaryManager";
    }


    @RequiresPermissions("dictionary:list")
    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   String keyword){
        IPage<Dictionary> page=new Page(pageNum,pageSize);
        QueryWrapper<Dictionary> wrapper=new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyword)){
            wrapper.like("d1.dictionary_name",keyword);
            wrapper.or();
            wrapper.like("d2.dictionary_name",keyword);
        }
        IPage<Dictionary> dictionaryIPage = dictionaryService.selectOwnConnect(page, wrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",dictionaryIPage.getTotal());
        map.put("rows",dictionaryIPage.getRecords());
        return map;
    }

    @RequiresPermissions("dictionary:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public ResultCode delete(Integer dictionaryId){
        boolean b = dictionaryService.removeById(dictionaryId);
        ResultCode resultCode =null;
        if (b){
            resultCode = new ResultCode(1, "删除成功");
        }else{
            resultCode = new ResultCode(0,"删除失败");
        }
        return resultCode;
    }

    @RequestMapping("/dictionaryEdit")
    public String dictionaryEdit(Model model,Integer dictionaryId){
        if (dictionaryId!=null){
            Dictionary dictionary = dictionaryService.getById(dictionaryId);
            model.addAttribute("dictionary",dictionary);
        }
        QueryWrapper<Dictionary> wrapper=new QueryWrapper<>();
        wrapper.isNull("parent_id");
        List<Dictionary> list = dictionaryService.list(wrapper);
        model.addAttribute("dictionaryTypes",list);
        return "dictionaryEdit";
    }


    @RequiresPermissions("dictionary:insert")
    @RequestMapping("/insert")
    @ResponseBody
    public ResultCode insert(Dictionary dictionary){
        boolean save = dictionaryService.saveOrUpdate(dictionary);
        ResultCode resultCode=null;
        if (save){
            resultCode=new ResultCode(1,"添加成功");
        }else{
            resultCode=new ResultCode(0,"添加失败");
        }
        return resultCode;
    }

    @RequiresPermissions("dictionary:update")
    @RequestMapping("/update")
    @ResponseBody
    public ResultCode update(Dictionary dictionary){
        boolean b = dictionaryService.saveOrUpdate(dictionary);
        ResultCode resultCode=null;
        if (b){
            resultCode=new ResultCode(1,"更新成功");
        }else{
            resultCode=new ResultCode(0,"更新失败");
        }
        return resultCode;
    }

}

